package com.foreknow.elm.controller;

import com.foreknow.elm.po.Cart;
import com.foreknow.elm.service.CartService;
import com.foreknow.elm.service.impl.CartServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class CartController {
    /**
     * 添加购物车
     * http://localhost:8080/elm/CartController/saveCart?foodId=3&businessId=10001&userId=8080
     * @param request
     * @return
     */
    public Object saveCart(HttpServletRequest request) {
        Cart cart = new Cart();
        cart.setFoodId(Integer.parseInt(request.getParameter("foodId")));
        cart.setBusinessId(Integer.parseInt(request.getParameter("businessId")));
        cart.setUserId(request.getParameter("userId"));
        CartService service = new CartServiceImpl();
        int result = service.saveCart(cart);
        return result;
    }

    /**
     * 修改数量
     * http://localhost:8080/elm/CartController/updateCart?foodId=3&businessId=10001&userId=8080&quantity=3
     * @param request
     * @return
     */
    public Object updateCart(HttpServletRequest request) {
         CartService service = new CartServiceImpl();
         Cart cart = new Cart();
         cart.setFoodId(Integer.parseInt(request.getParameter("foodId")));
         cart.setBusinessId(Integer.parseInt(request.getParameter("businessId")));
         cart.setUserId(request.getParameter("userId"));
         cart.setQuantity(Integer.parseInt(request.getParameter("quantity")));
         int result = service.updateCart(cart);
        return result;
    }

    /**
     * 删除
     * http://localhost:8080/elm/CartController/removeCart?userId=8080&businessId=10001&foodId=3
     * @param request
     * @return
     */
    public Object removeCart(HttpServletRequest request) {
        CartService service = new CartServiceImpl();
        Cart cart = new Cart();
        cart.setUserId(request.getParameter("userId"));
        cart.setBusinessId(Integer.parseInt(request.getParameter("businessId")));
        cart.setFoodId(Integer.parseInt(request.getParameter("foodId")));
        int result = service.removeCart(cart);
        return result;
    }

    /**
     * 根据条件查询购物车列表
     * http://localhost:8080/elm/CartController/listCart?userId=8080&businessId=10001
     * @param request
     * @return
     */
    public Object listCart(HttpServletRequest request) {
        CartService service = new CartServiceImpl();
        Cart cart = new Cart();
        cart.setUserId(request.getParameter("userId"));
        cart.setBusinessId(Integer.parseInt(request.getParameter("businessId")));
        List<Cart> list = service.listCart(cart);
        return list;
    }
}
